package com.xmindguoguo.boot.modular.system.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.xmindguoguo.boot.common.annotation.OperationLog;
import com.xmindguoguo.boot.common.constant.GlobalConstant;
import com.xmindguoguo.boot.common.enumtype.OperationLogType;
import com.xmindguoguo.boot.core.BaseController;
import com.xmindguoguo.boot.core.shiro.ShiroKit;
import com.xmindguoguo.boot.modular.system.model.TSystemNoticeModel;
import com.xmindguoguo.boot.modular.system.service.ITSystemNoticeService;
import com.xmindguoguo.boot.util.CmUtil;
import com.xmindguoguo.boot.util.ConvertUtil;
import com.xmindguoguo.boot.util.json.JsonResult;

/**
 * 控制器 //先暂时全部小写，后面写个方法注册进来，调整大小写
 * 
 * @author xmind-果果
 * @Date 2019-02-15 22:35
 */

@Controller
@RequestMapping("/tSystemNotice")
public class TSystemNoticeController extends BaseController {

    // 所属子模块名称+当前功能名称
    private String PREFIX = "/system/tSystemNotice/";

    @Autowired
    private ITSystemNoticeService systemNoticeService;

    /**
     * 跳转列表页
     */
    @RequestMapping("toTSystemNoticeList")
    public String toTSystemNoticeList() {
        return PREFIX + "tSystemNoticeList";
    }

    /**
     * 查询列表数据 表格 数据时用，不用删除
     */
    @RequestMapping("tSystemNoticeList")
    @ResponseBody
    public Object tSystemNoticeList(TSystemNoticeModel model, @RequestParam(value = "page", required = false) Integer current,
            @RequestParam(value = "limit", required = false) Integer size) {
        Wrapper<TSystemNoticeModel> wrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(model.getTitle())) {
            wrapper.like("title", model.getTitle());
        }
        wrapper.ne("status", GlobalConstant.MODEL_DEL);
        wrapper.orderBy("createTime", false);
        Page<TSystemNoticeModel> page = new Page<>(current, size);
        return JsonResult.sendPage(systemNoticeService.selectPage(page, wrapper)); // 跳转到列表页
    }

    @RequestMapping("toTSystemNoticeAdd")
    public String toTSystemNoticeAdd() {
        return PREFIX + "tSystemNoticeAdd";
    }

    /**
     * 添加model
     */
    @OperationLog(logName = "添加通知公告", operationLogType = OperationLogType.INSERT)
    @RequestMapping("tSystemNoticeAdd")
    @ResponseBody
    public Object tSystemNoticeAdd(@Validated TSystemNoticeModel model, BindingResult result) {
        if (result.hasErrors()) {
            return sendValidatedMsg(result);
        }
        model.setId(CmUtil.getPkId());
        model.setStatus(GlobalConstant.MODEL_ADD);
        model.setCreateTime(new Date());
        model.setCreateUserId(ShiroKit.getUserId());
        boolean flag = systemNoticeService.insert(model);
        return flag ? JsonResult.sendOk("添加成功", null) : JsonResult.sendError("添加失败", null);
    }

    @RequestMapping("toTSystemNoticeUpdate")
    public String toTSystemNoticeUpdate(Long id) {
        TSystemNoticeModel model = systemNoticeService.selectById(id);
        request.setAttribute("model", model);
        return PREFIX + "tSystemNoticeUpdate";
    }

    /**
     * 更新model
     */
    @OperationLog(logName = "更新通知公告", operationLogType = OperationLogType.UPDATE)
    @RequestMapping("tSystemNoticeUpdate")
    @ResponseBody
    public Object tSystemNoticeUpdate(@Validated TSystemNoticeModel model, BindingResult result) {
        if (result.hasErrors()) {
            return sendValidatedMsg(result);
        }
        model.setUpdateTime(new Date());
        model.setUpdateUserId(ShiroKit.getUserId());
        boolean flag = systemNoticeService.updateById(model);
        return flag ? JsonResult.sendOk("更新成功", null) : JsonResult.sendError("更新失败", null);
    }

    @OperationLog(logName = "删除通知公告", operationLogType = OperationLogType.DELETE)
    @RequestMapping("tSystemNoticeDelete")
    @ResponseBody
    public Object tSystemNoticeDelete(Long id) {
        TSystemNoticeModel model = new TSystemNoticeModel();
        model.setStatus(GlobalConstant.MODEL_DEL);
        model.setId(id);
        boolean flag = systemNoticeService.updateById(model);
        return flag ? JsonResult.sendOk("删除成功", null) : JsonResult.sendError("删除失败", null);
    }

    @OperationLog(logName = "批量删除通知公告", operationLogType = OperationLogType.DELETE)
    @RequestMapping("tSystemNoticeBatchDel")
    @ResponseBody
    public Object tSystemNoticeBatchDel(String ids) {
        Long[] idsArray = ConvertUtil.StringToLongArray(ids);
        List<TSystemNoticeModel> list = new ArrayList<>();
        TSystemNoticeModel model = null;
        for (Long id : idsArray) {
            model = new TSystemNoticeModel();
            model.setStatus(GlobalConstant.MODEL_DEL);
            model.setId(id);
            list.add(model);
        }
        boolean flag = systemNoticeService.updateBatchById(list);
        return flag ? JsonResult.sendOk("删除成功", null) : JsonResult.sendError("删除失败", null);
    }

    @RequestMapping("toTSystemNoticeDetail")
    public String toTSystemNoticeDetail(Long id) {
        TSystemNoticeModel model = systemNoticeService.selectById(id);
        request.setAttribute("model", model);
        return PREFIX + "tSystemNoticeDetail";
    }
}